package com.lhkj.iams.wechat.controller;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyV3Result;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.lhkj.iams.base.ro.ActionResult;
import com.lhkj.iams.wechat.config.mp.bo.WxParsePayResultBo;
import com.lhkj.iams.wechat.model.dto.WxJsapiOrderRequest;
import com.lhkj.iams.wechat.service.WxMpPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.Serializable;
/**
  * <p>
  *     描述：微信支付
  * </p>
  */
@Controller
@RequestMapping("api/iams/wechat/pay")
public class WxPayController implements Serializable {

    protected Logger log = LoggerFactory.getLogger(WxPayController.class);

    @Resource
    private WxMpPayService wxMpPayService;

    /**
     * 微信原生支付
     */
    @PostMapping("/jsApiOrder")
    @ResponseBody
    public ActionResult<WxPayUnifiedOrderV3Result.JsapiResult> jsApiOrder(@RequestBody WxJsapiOrderRequest payRequest) throws WxPayException {
        WxPayUnifiedOrderV3Result.JsapiResult result = wxMpPayService.jsApiOrder(payRequest);
        log.info("微信jsapi下单参数：{}", JSON.toJSONString(result));
        return ActionResult.success(result);
    }

    /**
     * 解析微信支付回调
     */
    @PostMapping("/parseOrderNotifyV3Result")
    @ResponseBody
    public ActionResult<WxPayNotifyV3Result.DecryptNotifyResult> parseOrderNotifyV3Result(@RequestBody WxParsePayResultBo parsePayResultBo) throws WxPayException, JsonProcessingException {
        return ActionResult.success(wxMpPayService.parseOrderNotifyV3Result(parsePayResultBo));
    }
}
